package com.cjg.shadowhostpro

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.Message
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.casic.titan.constant.Constant
import com.cjg.common_lib.IRemoteService
import com.cjg.shadowhostpro.ipc.RemoteService
import com.cjg.shadowhostpro.manager.PluginHelper
import com.tencent.shadow.dynamic.host.EnterCallback

class MainActivity : AppCompatActivity() {

    private var mBinder: IRemoteService.Stub? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val intent = Intent(this, RemoteService::class.java)
        bindService(
            intent, object : ServiceConnection {
                override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                    service?.let {
                        mBinder = service as IRemoteService.Stub
                    }
                }

                override fun onServiceDisconnected(name: ComponentName?) {
                    mBinder = null
                }

            }, BIND_AUTO_CREATE
        )
    }

    fun startPlugin(view: View) {
        PluginHelper.getInstance().singlePool.execute {
            MyApplication.getApp().loadPluginManager(PluginHelper.getInstance().pluginManagerFile)
            val bundle = Bundle()
            bundle.putString(
                Constant.KEY_PLUGIN_ZIP_PATH, PluginHelper.getInstance().pluginZipFile.absolutePath
            )
            bundle.putString(
                Constant.KEY_PLUGIN_NAME, Constant.PLUGIN_APP_NAME
            )// partKey 每个插件都有自己的 partKey 用来区分多个插件，如何配置在下面讲到
            bundle.putString(
                Constant.KEY_ACTIVITY_CLASSNAME, "com.cjg.plugin_app.PluginMainActivity"
            )//要启动的插件的Activity页面
            bundle.putBundle(Constant.KEY_EXTRAS, Bundle()) // 要传入到插件里的参数
            MyApplication.getApp().pluginManager.enter(
                this@MainActivity,
                Constant.FROM_ID_START_ACTIVITY,
                bundle,
                object : EnterCallback {
                    override fun onShowLoadingView(view: View?) {
                        Log.e("PluginLoad", "onShowLoadingView");
                    }

                    override fun onCloseLoadingView() {
                        Log.e("PluginLoad", "onCloseLoadingView");
                    }

                    override fun onEnterComplete() {
                        Log.e("PluginLoad", "onEnterComplete");
                    }
                })
        }
        post()
    }

    private val handler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (msg.what == 0) {
                val text = "宿主进程=====>${index}"
                Log.e("宿主进程=====>", text)
                mBinder?.apply { sendMessageToClient(text) }
//                instance.sendToPlugin(text)
                index++
                sendEmptyMessageDelayed(0, 1000)
            }
        }
    }
    var index = 0
    private fun post() {
        handler.sendEmptyMessageDelayed(0, 1000)
    }
}